<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Tab_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id_tab  = 0;  
	/**
	 * @var unknown_type
	 */
	var $id_parent_tab = 0 ;  
	/**
	 * @var unknown_type
	 */
	var $name_vi = null ; 
	/**
	 * @var unknown_type
	 */
	var $name_en = null ; 
	/**
	 * @var unknown_type
	 */
	var $class_name = null ; 
	/**
	 * @var unknown_type
	 */
	var $icon = null ; 
	/**
	 * @var unknown_type
	 */
	var $position = 0;
	/*
	 * @access	public
	 */
	function Tab_model()
	{
		parent::__construct();
		
	}
		
	function insert(){
		$sql = "INSERT INTO gtb_tab (id_parent_tab, vi_name, en_name, class_name, position, icon)
				VALUES(".
				intval($this->id_parent_tab).", " .
				$this->db->escape($this->vi_name).", " .
				$this->db->escape($this->en_name).", " .
				$this->db->escape($this->class_name).", " .
				intval($this->position).", " .
				$this->db->escape($this->icon).
				")";
				
		$this->db->query($sql);
		if($this->db->affected_rows())
			return $this->db->insert_id(); 
		return 0;
	}
	
	function update($tab){
		 $sql = "UPDATE gtb_tab
				SET ".
				"id_parent_tab = " .  intval($tab->id_parent_tab) . "," . 
				"vi_name = " .  $this->db->escape($tab->vi_name) . "," .
				"en_name = " .  $this->db->escape($tab->en_name) . ", " .
				"class_name = " .  $this->db->escape($tab->class_name) . ", " .
				"position = " .  intval($tab->position) . ", " .
				"icon = " .  $this->db->escape($tab->icon)  .
				"WHERE id_tab = " .intval($tab->id_tab);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = trim(implode(',', $id));
		if($ids=='') return;
		
		$sql = "SELECT icon FROM gtb_tab WHERE id_tab IN ($ids)";
		$query = $this->db->query($sql);
		$result = $query->result();
		
		if(count($result) > 0){
			for($i = 0; $i < count($result); $i++){
				if($result[$i]->icon!=''){
					@unlink($result[$i]->icon);
				}
			}
		}
				
		$sql = "DELETE FROM gtb_tab WHERE id_tab IN ($ids)";
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAll($id_parent = -1, $offset = 0, $limit=0){
		$result = null;
		if($limit)
			 $sql = 'SELECT t.*, t2.vi_name as parent_name, (SELECT COUNT(*) FROM gtb_tab t1 WHERE t1.id_parent_tab = t.id_tab) as num_child,
					(SELECT MAX(t3.position) FROM gtb_tab t3) as max_position, 
					(SELECT MIN(t4.position) FROM gtb_tab t4) as min_position 
				FROM gtb_tab t
				LEFT JOIN gtb_tab t2 ON t.id_parent_tab = t2.id_tab
				
				WHERE (t.id_parent_tab = '.intval($id_parent) . ' OR -1=' . intval($id_parent) . ')
				ORDER BY position 
				LIMIT ' . intval($offset) . ', ' . intval($limit);
		else
			$sql = 'SELECT t.*, t2.vi_name as parent_name, (SELECT COUNT(*) FROM gtb_tab t1 WHERE t1.id_parent_tab = t.id_tab) as num_child,
					(SELECT MAX(t3.position) FROM gtb_tab t3) as max_position, 
					(SELECT MIN(t3.position) FROM gtb_tab t3) as min_position 
				FROM gtb_tab t
				LEFT JOIN gtb_tab t2 ON t.id_parent_tab = t2.id_tab
				
				WHERE (t.id_parent_tab = '.intval($id_parent) . ' OR -1=' . intval($id_parent) . ')
				ORDER BY position';
		// LEFT JOIN ( SELECT COUNT(*) as num_child FROM gtb_tab t3 ON t3.id_parent_tab = t.id_tab)
		
		$query = $this->db->query($sql);
		$result = $query->result();
      
		return $result;
	}
	
	function getTabs($id){
		$sql = 'SELECT t.*
				FROM gtb_tab t			
				WHERE (t.id_parent_tab = 0) AND t.id_tab != '.intval($id).'
				ORDER BY t.position ASC';
		$query = $this->db->query($sql);
		$result = $query->result();
      
		return $result;
	}
	
	function getAllCount($id_parent = -1){
		$sql = 'SELECT COUNT(*) as num_record
				FROM gtb_tab t
				WHERE (t.id_parent_tab = '.intval($id_parent) . ' OR -1=' . intval($id_parent) . ')';
		
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->num_record;
		}
       
		return null;
		
	}
	
	function getAllByAccess($id_parent, $id_profile, $default_tab = ''){
		$result = null;
		/*$sql = 'SELECT t.id_tab id, a.id_profile, t.class_name, t.vi_name, t.en_name, t2.vi_name vi_parent, t2.en_name en_parent, t2.class_name class_parent, t.icon
				FROM gtb_tab t
				INNER JOIN gtb_access a ON a.id_tab = t.id_tab
			    INNER JOIN gtb_tab t2 ON t.id_tab = t2.id_tab
				WHERE t.id_parent_tab = '. intval($id_parent) . 
					' AND a.id_profile = '.intval($id_profile).'
				ORDER BY t.position';
		*/
		$sql = 'SELECT t.id_tab id, a.id_profile, t.class_name, t.vi_name, t.en_name, t2.vi_name vi_parent, t2.en_name en_parent, t2.class_name class_parent, t.icon, t3.class_name sub_default
				FROM gtb_tab t
				INNER JOIN gtb_access a ON a.id_tab = t.id_tab
			    INNER JOIN gtb_tab t2 ON t.id_tab = t2.id_tab
			    LEFT JOIN gtb_tab t3 ON (t.id_tab = t3.id_parent_tab AND t3.class_name = "'.$default_tab .'")
				WHERE t.id_parent_tab = '. intval($id_parent) . 
					' AND a.id_profile = '.intval($id_profile).
					' AND (view=1 OR edit=1 OR `delete`=1 OR `add`=1) 
				ORDER BY t.position';
		//echo '<br><br>' . $sql . '<br><br>';
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition(){
		
		$sql = "SELECT MAX(position) as position_max
				FROM gtb_tab ";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max ;
		}
       
		return 0;
	}
	
	function getById($id_tab){
		$sql = 'SELECT *
				FROM gtb_tab t
				WHERE t.id_tab = ' . intval($id_tab);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	
		
//	
}

// EOF
